package com.atguigu.ssm.handler;

import com.atguigu.ssm.entity.Employee;
import com.atguigu.ssm.service.api.EmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class EmployeeHandler {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/employee", method = RequestMethod.GET)
    public String showAllEmployee(Model model) {

        List<Employee> employeeList = employeeService.getAll();
        model.addAttribute("employeeList", employeeList);

        return "emp-list";
    }

    // /employee/page/1
    // /employee/page/2
    // /employee/page/10
    // /employee/page/500
    @RequestMapping("/employee/page/{pageNo}")
    public String showPage(Model model, @PathVariable("pageNo") Integer pageNo) {

        // 调用 Service 方法返回分页数据的查询结果（封装到 PageInfo 对象中）
        PageInfo<Employee> pageInfo = employeeService.getPage(pageNo);

        // 将 PageInfo 对象存入模型
        model.addAttribute("pageInfo", pageInfo);

        return "emp-page";
    }
}
